<?php

namespace app\model;

use app\model\Base;


class Zhusu extends Base
{
    /**
     *定义主键
     * @var string 
     */
    protected $pk = 'id';

    /**
     * 分页列表
     * @return array
     */
    public function listQuery($groupid)
    {
        if($groupid==1||$groupid==9||$groupid==11){$where=['isdel'=>0];}
        else{$where = ['isdel'=>0,'groupid'=> $groupid];}
       $data =  $this->where($where)->field('id,order_id,product_id,number,unit,material_id,colour_id,order_number,qualified_quantity,working_hours,scrapped_part,operator,accumulate_number,debugging_time,change_material_time,abnormal_time,weight,remarks,classes')->order('id', 'desc')->paginate(input('limit/d'));
       foreach ($data as $key => $value) {
      
       if($data[$key]['classes']==1){ $data[$key]['class'] ="早班";}else if($data[$key]['classes'] = 2){$data[$key]['class'] ="晚班";}
 
        $data[$key]['product']     = $data[$key]['product_id'];
        $data[$key]['material']    = $data[$key]['material_id'];
        $data[$key]['colour']      = $data[$key]['colour_id'];
        $data[$key]['operator_id'] = $data[$key]['operator'];

        $data[$key]['product_id']  = Product::where(['id'=>$data[$key]['product_id']])->column('name');
        $data[$key]['material_id'] = Material::where(['id'=>$data[$key]['material_id']])->column('name');
        $data[$key]['colour_id']   = Colour::where(['id'=>$data[$key]['colour_id']])->column('name');
        $data[$key]['operator']    =  system\Manager::where(['userid'=>$data[$key]['operator']])->column('truename');

      }
       return $data;
    }

   
    /**
     * 软删除数据
     * @param  type  $where   查询条件
     * @return int   删除的条数，0表示未删除任何数据
     */
    public static function isdel($where)
    {
        return self::where($where)->where(['isexamine'=>0])->save(['isdel'=>1]);
    }
    
    /**
     * 今日数据分页列表
     * @return array
     */
    public function today_listQuery($roleid,$groupid)
    {
        
        $t = date("Y-m-d");
        $where[] = ['scdate','>=',strtotime($t." 00:00:00")];
        $where[] = ['scdate','<=',strtotime($t." 23:59:59")];
        $where[] = ['isdel','=',0];
        if($roleid!=1){$where[] = ['isexamine','=',$roleid];}
        if($groupid == 17){$where[] = ['groupid','=',13];}
        else if($groupid == 18){$where[] = ['groupid','=',14];}
        else if($groupid == 19){$where[] = ['groupid','=',15];}
       $data =  $this->where($where)->field('id,order_id,product_id,number,unit,material_id,colour_id,order_number,qualified_quantity,working_hours,scrapped_part,operator,accumulate_number,debugging_time,change_material_time,abnormal_time,weight,remarks,classes')->order('id', 'desc')->paginate(input('limit/d'));
       foreach ($data as $key => $value) {

        if($data[$key]['classes']==1){ $data[$key]['class'] ="早班";}else if($data[$key]['classes'] = 2){$data[$key]['class'] ="晚班";}
        $data[$key]['product']     = $data[$key]['product_id'];
        $data[$key]['material']    = $data[$key]['material_id'];
        $data[$key]['colour']      = $data[$key]['colour_id'];
        $data[$key]['operator_id'] = $data[$key]['operator'];

        $data[$key]['product_id']  = Product::where(['id'=>$data[$key]['product_id']])->column('name');
        $data[$key]['material_id'] = Material::where(['id'=>$data[$key]['material_id']])->column('name');
        $data[$key]['colour_id']   = Colour::where(['id'=>$data[$key]['colour_id']])->column('name');
        $data[$key]['operator']    =  system\Manager::where(['userid'=>$data[$key]['operator']])->column('truename');

      }
       return $data;
    }
    /**
     * 今日数据统计分页列表
     * @return array
     */
    public function statistics_today_listQuery($roleid,$groupid)
    {
        
        $t = date("Y-m-d");
        $where[] = ['scdate','>=',strtotime($t." 00:00:00")];
        $where[] = ['scdate','<=',strtotime($t." 23:59:59")];
        $where[] = ['isdel','=',0];
        if($roleid!=1){$where[] = ['isexamine','=',3];}
        if($groupid == 17){$where[] = ['groupid','=',13];}
        else if($groupid == 18){$where[] = ['groupid','=',14];}
        else if($groupid == 19){$where[] = ['groupid','=',15];}
        $where[] = ['classes','=',1];
       $data_number =  $this->where($where)->field('id,order_id,product_id,number,working_hours,scrapped_part,accumulate_number,debugging_time,change_material_time,abnormal_time,classes')->order('number', 'asc')->select();

       foreach ($data_number as $key => $v) {
    
      
        $data[$key]['number'] = $data_number[$key]['number'];
        $data[$key]['order_id'] = $data_number[$key]['order_id'];
        $data[$key]['working_hours'] = $data_number[$key]['working_hours'];
        $data[$key]['scrapped_part']= $data_number[$key]['number'];
        $data[$key]['accumulate_number']= $data_number[$key]['accumulate_number'];
        $data[$key]['debugging_time']= $data_number[$key]['debugging_time'];
        $data[$key]['change_material_time']= $data_number[$key]['change_material_time'];
        $data[$key]['abnormal_time']= $data_number[$key]['abnormal_time'];
        $a[$key] = Product::where(['id'=>$data_number[$key]['product_id']])->column('modulus');
        $data[$key]['mo_shu']  = $data_number[$key]['accumulate_number']*$a[$key][0];
        if($data_number[$key]['classes']==1){$data[$key]['class'] ="早班";}else if($data_number[$key]['classes'] = 2){$data[$key]['class'] ="晚班";}
        $data[$key]['product_id']  = Product::where(['id'=> $data_number[$key]['product_id']])->column('name');

       }
     
       return $data;
    }

        /**
     * 筛选今日数据统计分页列表
     * @return array
     */
    public function screen_today_listQuery($roleid,$groupid)
    {
        
        $t = date("Y-m-d");
        $where[] = ['scdate','>=',strtotime($t." 00:00:00")];
        $where[] = ['scdate','<=',strtotime($t." 23:59:59")];
        $where[] = ['isdel','=',0];
        if($roleid!=1){$where[] = ['isexamine','=',3];}
        if($groupid == 17){$where[] = ['groupid','=',13];}
        else if($groupid == 18){$where[] = ['groupid','=',14];}
        else if($groupid == 19){$where[] = ['groupid','=',15];}

       $data_number =  $this->where($where)->field('id,order_id,product_id,number,working_hours,scrapped_part,accumulate_number,debugging_time,change_material_time,abnormal_time,classes')->group('number')->order('number', 'asc')->select();

       foreach ($data_number as $key => $v) {
    
        $data[$key]['xiaji']  =$this->where(['number'=>$data_number[$key]['number']])->order('id', 'desc')->select();
        $data[$key]['number'] = $data_number[$key]['number'];
        $data[$key]['order_id'] = $data_number[$key]['order_id'];
        $data[$key]['working_hours'] = $data_number[$key]['working_hours'];
        $data[$key]['scrapped_part']= $data_number[$key]['number'];
        $data[$key]['accumulate_number']= $data_number[$key]['scrapped_part'];
        $data[$key]['debugging_time']= $data_number[$key]['debugging_time'];
        $data[$key]['change_material_time']= $data_number[$key]['change_material_time'];
        $data[$key]['abnormal_time']= $data_number[$key]['abnormal_time'];
        $a[$key] = Product::where(['id'=>$data_number[$key]['product_id']])->column('modulus');
        $data[$key]['mo_shu']  = $data_number[$key]['accumulate_number']*$a[$key][0];
        if($data_number[$key]['classes']==1){$data[$key]['class'] ="早班";}else if($data_number[$key]['classes'] = 2){$data[$key]['class'] ="晚班";}
        $data[$key]['product_id']  = Product::where(['id'=> $data_number[$key]['product_id']])->column('name');

       }
     
       return $data;
    }
}